import jwt from 'jsonwebtoken'
import Conf from '@/config'
import to from '@/utils/to'

import * as Model from '../models'
const {
    Login: UserLoginModel,
    Register: UserRegisterModel,
    Update: UserUpdateModel,
    Logout: UserLogoutModel
} = Model


class ServiceUser {
    static async login(password, username) {
        console.log(Model);
        const [err1, res] = await to(UserLoginModel.findByUserName(username))
        if (err1 || (res && res.length == 0)) {
            return {
                msg: '用户名不存在',
                code: 400,
                data: null
            };
        }
        const [err2, userInfo] = await to(UserLoginModel.findByUserNameAndPassword(username, password))
        if (err2 || !!!userInfo) {
            return {
                msg: '密码错误',
                code: 400,
                data: null
            };
        }
        /* 生成token */

        let token = jwt.sign({
            id: userInfo.id,
            username: userInfo.username
        }, Conf.jwt.jwtSecret, {
            expiresIn: Conf.jwt.expiresIn
        })
        jwt.decode(token)
        return {
            msg: '验证通过',
            code: 200,
            data: {
                token: "Bearer " + token,
                userInfo: {
                    id: userInfo.id,
                    username: userInfo.username
                }
            }
        };

    }

    static async register({
        password,
        username,
        nickname,
        phone,
        role
    }) {
        let [err1, users] = await to(UserLoginModel.findByUserName(username))
        if (err1 || users && users.length > 0) {
            return {
                msg: '该用户名已注册',
                code: 400,
                data: null
            };
        }
        let [err2, newuser] = await to(UserRegisterModel.registerNewUser({
            password,
            username,
            nickname,
            phone,
            role
        }))
        if (err2) {
            return {
                msg: '注册失败',
                code: 400,
                data: null
            };
        }
        return {
            msg: '注册成功',
            code: 200,
            data: {
                username
            }
        };

    }

    static async update({
        id,
        password = '',
        username = '',
        nickname = '',
        phone = '',
        role = ''
    }) {
        const [err, users] = await to(UserUpdateModel.updateUserInfo(id))
        if (err) {
            return {
                msg: '修改失败',
                code: 400,
                data: null
            };
        }
        users.password = password || users.password
        users.username = username || users.username
        users.nickname = nickname || users.nickname
        users.phone = phone || users.phone
        users.role = role || users.role
        const [err2, save] = await to(users.save())
        if (err2) {
            return {
                msg: '修改失败',
                code: 400,
                data: null
            };
        }
        return {
            msg: '修改成功',
            code: 200,
            data: users
        };

    }

    static async logout(id) {
        const [err, user] = await to(UserLogoutModel.LogoutUser(id))
        const [err2, des] = await to(user.destroy())
        if (err || err2) {
            return {
                msg: '注销用户失败',
                code: 400,
                data: null
            };
        }
        return {
            msg: '注销用户成功',
            code: 200,
            data: null
        };
    }
}
export default ServiceUser